package com.rongzhe.house.presenter;

import android.content.Intent;
import android.text.TextUtils;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.voo.HouseOverview;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.ui.FilterChangeListener;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.ui.activity.HouseSearchActivity;

import java.util.List;


/**
 * Created by zhongyanli on 2017/11/12.
 */

public class HouseSearchPresenter extends BasePresenter
        implements FilterChangeListener {
    private PageContoller<HouseOverview> mPageController;

    public HouseSearchPresenter(UiControlInterface uiControlInterface) {
        super(uiControlInterface);
    }

    @Override
    public void start(Intent intent) {
        mPageController = new PageContoller<HouseOverview>(
                (HouseSearchActivity)getUiControlInterface());
        loadData(false);
    }

    @Override
    public void onFilterChanged() {
        ((HouseSearchActivity)getUiControlInterface()).resetToTop();
        mPageController.resetPage();
        loadData(false);
    }

    public void loadData(final boolean append) {
        getUiControlInterface().showProgress(mContext.getString(R.string.loading));
        HouseManager.getInstance().queryHouse(mPageController.getMaxDataOnePage(),
                mPageController.getCurrentPage(),
                new DataListener<List<HouseOverview>>() {
                    @Override
                    public void onSuccess(List<HouseOverview> data, String message) {
                        mPageController.onDataLoaded(data, append);
                    }

                    @Override
                    public void onFailed(Throwable error, String message) {
                        getUiControlInterface().showToast(message);
                    }

                    @Override
                    public void onEnd() {
                        getUiControlInterface().dismissProgress();
                        mPageController.onDataLoadFinish(append);
                    }
                });
    }

    public void setKeyWords(String keyWords) {
        mPageController.resetPage();
        if (TextUtils.isEmpty(keyWords)) {
            keyWords = null;
        }
        HouseManager.getInstance().setKeywords(keyWords);
        loadData(false);
    }
}
